<template>
  <div>
    <el-dialog
      title="分配用户"
      :closeOnClickModal="false"
      :visible.sync="show"
      width="540px">
      <el-form 
        label-position="right" 
        :model="selectTable" 
        ref="addForm" 
        label-width=""
        autocomplete="off"
        size="medium">
        <el-form-item label="角色:">
          {{this.selectTable.roleName}}
        </el-form-item>
        <el-transfer
          filterable
          :filter-method="filterMethod"
          filter-placeholder="请输入关键词"
          :titles="['未选择','已选择']"
          :props="{
            key: 'userId',
            label: 'userAccount'
          }"
          v-model="userDataEd"
          :data="userData">
        </el-transfer>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {roleUserList,roleSetUser} from '@/utils/api'
export default {
  components: {
  },
  props: {
    data: Object
  },
  created () {
    this.selectTable =  this.JSONs(this.data)
    this.roleId = this.data.roleId
    this.getList();
  },
  watch: {
    show (newVal) {
      this.$parent.roleAssignAccount = false;
    }
  },
  computed: {
  },
  data () {
    return {
      pload: false,
      rule: [
        { required: true, message: '请选择一个选项', trigger: 'blur' },
        { required: true, message: '必填项', trigger: 'blur' }
      ],
      show: true,
      selectTable: {},
      userData: [],
      userDataEd: [],
      filterMethod(query, item) {
        return item.userAccount.indexOf(query) > -1;
      }
    }
  },
  methods: {
    async getList () {
      let datas = await roleUserList({roleId: this.roleId});
      this.userData = datas.noRoleUsers
      this.userDataEd = datas.roleUsers
    },
    async submitForm(formName) {
      await this.$refs[formName].validate();
      try {
        this.pload = true
        let ids = this.userDataEd.join(',');
        await roleSetUser({roleId:this.roleId,userIds:ids});
        this.$notify({title: '操作成功', type: 'success', duration: 5000})
        this.$parent.roleAssignAccount = false;
        this.$emit('getData');
      } catch(e) {
        this.pload = false
      }
    }
  }
}
</script>